<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Update Menu</title>
    <link rel="stylesheet" th:href="@{/plugins/layui/css/layui.css}">
</head>
<body>
<!-- 表单使用 th:object 绑定对象属性 -->
<form th:object="${permission}" class="layui-form">
    <!-- 隐藏字段 id -->
    <input type="hidden" id="id" th:field="*{id}">

    <!-- 权限名称 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="menuName" lay-verify="required" autocomplete="off" lay-affix="clear" th:field="*{menuName}" class="layui-input">
            </div>
        </div>
    </div>

    <!-- 菜单类型 -->
    <div class="layui-form-item">
        <input type="hidden" id="menuType" value="" lay-verify="required" autocomplete="off" lay-affix="clear" th:field="*{menuType}" class="layui-input">
        <label class="layui-form-label">菜单类型</label>
        <div class="layui-input-block">
            <!-- 菜单类型选择按钮 -->
            <input type="radio" lay-filter="menuType-filter" name="menuType" value="M" class="layui-input" title="目录" th:checked="*{menuType == 'M'}">
            <input type="radio" lay-filter="menuType-filter" name="menuType" value="C" class="layui-input" title="菜单" th:checked="*{menuType == 'C'}">
            <input type="radio" lay-filter="menuType-filter" name="menuType" value="F" class="layui-input" title="按钮" th:checked="*{menuType == 'F'}">
        </div>
    </div>

    <!-- 父级菜单 -->
    <div class="layui-form-item" id="pIdFormItem">
        <label class="layui-form-label">父级菜单</label>
        <div class="layui-input-inline layui-input-wrap">
            <select id="pId" >
                <option value="">请选择</option>
                <!-- Here, options will be populated dynamically via JavaScript -->
            </select>
        </div>
    </div>

    <!-- 请求地址 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">请求地址</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="url" class="layui-input" th:field="*{url}" lay-verify="required" autocomplete="off">
            </div>
        </div>
    </div>

    <!-- 权限标识 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">权限标识</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="perms" lay-verify="required" autocomplete="off" th:field="*{perms}" class="layui-input">
            </div>
        </div>
    </div>
</form>

<!-- 引入必要的JS文件 -->
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/plugins/layui/layui.js}"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var $ = layui.$;

        // 初始化表单字段
        var menuType = $("#menuType").val();
        var permissionId = $("#id").val();

        // 监听菜单类型选择
        form.on('radio(menuType-filter)', function(data){
            let menuType = data.elem.value;
            $("#menuType").val(menuType);
            if (menuType == "M") {
                $("#pIdFormItem").hide();
                $("#url").val("#").attr("disabled", true);
                $("#perms").val("").attr("disabled", true);
            } else if(menuType == "C") {
                $("#pIdFormItem").show();
                $("#url").val("").removeAttr("disabled");
                $("#perms").val("").removeAttr("disabled");
                getListMenu("M");
            } else if(menuType == "F"){
                $("#pIdFormItem").show();
                $("#url").val("").removeAttr("disabled");
                $("#perms").val("").removeAttr("disabled");
                getListMenu("C");
            }
        });

        // 通过AJAX获取父级菜单列表
        function getListMenu(menuType) {
            $.ajax({
                type: "get",
                url: "/permission/listMenu",
                data: { menuType: menuType },
                dataType: "json",
                success: function (data) {
                    $("#pId").empty();
                    $("#pId").append("<option value=''>请选择</option>");
                    data.forEach(function (menu) {
                        var option = $("<option>").val(menu.id).text(menu.menuName);
                        if (menu.id == $("#pId").val()) {
                            option.attr("selected", "selected");
                        }
                        $("#pId").append(option);
                    });
                    form.render('select'); // 刷新select控件
                }
            })
        }

        // 当页面加载完成时根据 menuType 设置父级菜单的选择
        if (menuType === 'C' || menuType === 'F') {
            $("#pIdFormItem").show();
            getListMenu(menuType === 'C' ? "M" : "C");
        } else {
            $("#pIdFormItem").hide();
            $("#url").val("#").attr("disabled", true);
            $("#perms").val("").attr("disabled", true);
        }
    });
</script>
</body>
</html>
